home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / inc / iritprsr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-02  |  13.8 KB  |  316 lines

  1. /*****************************************************************************
  2. * Generic parser for the "Irit" solid modeller.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Sep. 1991   *
  5. *****************************************************************************/
  6.  
  7. #ifndef    IRIT_PRSR_GH
  8. #define    IRIT_PRSR_GH
  9.  
  10. #include <setjmp.h>
  11.  
  12. #include "irit_sm.h"
  13. #include "cagd_lib.h"
  14. #include "genmat.h"
  15.  
  16. /* Dont change the order of these objects (or there values as overloaded     */
  17. /* tables (see overload.c) are hardwired to it. If you add objects update    */
  18. /* that module properly.                             */
  19. typedef enum {
  20.     IP_OBJ_UNDEF = 0,
  21.  
  22.     IP_OBJ_POLY,                     /* These are the objects in overload.c. */
  23.     IP_OBJ_NUMERIC,
  24.     IP_OBJ_POINT,
  25.     IP_OBJ_VECTOR,
  26.     IP_OBJ_PLANE,
  27.     IP_OBJ_MATRIX,
  28.     IP_OBJ_CURVE,
  29.     IP_OBJ_SURFACE,
  30.     IP_OBJ_STRING,
  31.     IP_OBJ_LIST_OBJ,
  32.     IP_OBJ_CTLPT,
  33.  
  34.     IP_OBJ_AUX_ATTR = 50,     /* These are the auxiliary objects/structs. */
  35.     IP_OBJ_AUX_VERTEX,
  36.     IP_OBJ_AUX_POLY,
  37.     IP_OBJ_AUX_CURVE,
  38.     IP_OBJ_AUX_SURFACE,
  39.     IP_OBJ_AUX_MATRIX,
  40.     IP_OBJ_AUX_STRING,
  41.     IP_OBJ_AUX_OLST,
  42.     IP_OBJ_AUX_END,
  43.  
  44.     ANY_OBJ = 100         /* Match any object type, in type checking. */
  45. } IPObjStructType;
  46.  
  47. typedef enum {             /* Possible error code during data parsing. */
  48.     IP_NO_ERR = 0,
  49.  
  50.     IP_ERR_NUMBER_EXPECTED,
  51.     IP_ERR_OPEN_PAREN_EXPECTED,
  52.     IP_ERR_CLOSE_PAREN_EXPECTED,
  53.     IP_ERR_LIST_COMP_UNDEF,
  54.     IP_ERR_UNDEF_EXPR_HEADER,
  55.     IP_ERR_PT_TYPE_EXPECTED,
  56.     IP_ERR_OBJECT_EMPTY,
  57.     IP_ERR_FILE_EMPTY,
  58.     IP_ERR_MIXED_TYPES,
  59.     IP_STR_NOT_IN_QUOTES,
  60.     IP_ERR_OBJECT_EXPECTED,
  61.     IP_ERR_CAGD_LIB_ERR,
  62.     IP_ERR_STACK_OVERFLOW,
  63.     IP_ERR_DEGEN_POLYGON,
  64.     IP_ERR_DEGEN_NORMAL,
  65.     IP_ERR_SOCKET_BROKEN,
  66.     IP_ERR_SOCKET_TIME_OUT,
  67.     IP_ERR_BIN_UNDEF_OBJ,
  68.  
  69.     IP_WRN_OBJ_NAME_TRUNC = 100
  70. } IritPrsrErrType;
  71.  
  72. #define IP_LOAD_COLOR           14  /* Index color default - loaded objects. */
  73.  
  74. #define IRIT_DATA_HEADER(File, Name) \
  75.     fprintf(File, "Irit %s, %s,\nCreator: %s,\nDate: %s.\n\n", \
  76.         VERSION, COPYRIGHT, Name, IritRealTimeDate());
  77.  
  78. /*****************************************************************************
  79. * Global data structures:                             *
  80. * Objects in the system might be (real) scalars, (R3) vectors, matrices      *
  81. * (4 by 4 - transformation matrix), strings of chars, lists of objects, or   *
  82. * geometric objects. All but the last are simple and all their data is saved *
  83. * in the object space itself. The last (geometric) object points on a         *
  84. * curve or a surface or a polygonal list of the form:                 *
  85. *                                         *
  86. * Polygon -> Polygon -> Polygon -> Polygon -> .... -> NULL             *
  87. *    |        |       |          |                         *
  88. *    V          V          V          V                         *
  89. *  VList      VList      VList      VList    (VList = Vertex List)         *
  90. *                                         *
  91. * Each VList is a CIRCULAR vertex list. Each VList element (IPVertexStruct)  *
  92. * implicitly defines an edge from this vertex, to the next. As each edge     *
  93. * is used by exactly two polygons, a pointer to the other polygon using this *
  94. * edge exists in the IPVertexStruct. Each polygon has also its Plane         *
  95. * definition for fast processing, with its normal pointing INTO the object.  *
  96. *   Few other tags & flags are included in the data structures for different *
  97. * modules.                                     *
  98. *   Note, vertices are not shared by few VLists/Polygons although it may     *
  99. * decrease memory usage (suprisingly, not much). The main reason to that is  *
  100. * the basic assumption of this solid modeller, which is simplicity...         *
  101. *****************************************************************************/
  102.  
  103. /*****************************************************************************
  104. * Vertex Type - holds single 3D point, including some attributes on it as    *
  105. * Tags. The 3D coordinates are saved in Pt. Pointer to next in chain         *
  106. * is Pnext, and the pointer to the adjacent polygon (to the edge defined by  *
  107. * this Vertex and Vertex -> Pnext) is PAdj.                     *
  108. *****************************************************************************/
  109.  
  110. /* Internal edge, or edge generated by the polygon decomposition stage when  */
  111. /* only convex polygons are allowed. This edge was not in the input         */
  112. /* non-convex polygon, and therefore one may not want to see/display it.     */
  113. /* Note bits 4-7 (high nibble of Tags) are reserved for the different         */
  114. /* modules to perform their local tasks and so should not be used here.         */
  115. #define IP_VRTX_INTERNAL_TAG    0x01    /* Internal Tag - Edge is internal.  */
  116. #define IP_VRTX_NORMAL_TAG    0x02     /* Normal Tag - Vertex has normal.  */
  117.  
  118. #define IP_IS_INTERNAL_VRTX(Vrtx)    (Vrtx -> Tags & IP_VRTX_INTERNAL_TAG)
  119. #define IP_SET_INTERNAL_VRTX(Vrtx)    (Vrtx -> Tags |= IP_VRTX_INTERNAL_TAG)
  120. #define IP_RST_INTERNAL_VRTX(Vrtx)    (Vrtx -> Tags &= IP_VRTX_INTERNAL_TAG)
  121. #define IP_HAS_NORMAL_VRTX(Vrtx)    (Vrtx -> Tags & IP_VRTX_NORMAL_TAG)
  122. #define IP_SET_NORMAL_VRTX(Vrtx)    (Vrtx -> Tags |= IP_VRTX_NORMAL_TAG)
  123. #define IP_RST_NORMAL_VRTX(Vrtx)    (Vrtx -> Tags &= IP_VRTX_NORMAL_TAG)
  124.  
  125. typedef struct IPVertexStruct {
  126.     struct IPVertexStruct *Pnext;                /* To next in chain. */
  127.     struct IPAttributeStruct *Attrs;
  128.     struct IPPolygonStruct *PAdj;             /* To adjacent polygon. */
  129.     PointType Coord;                   /* Holds X, Y, Z coordinates. */
  130.     NormalType Normal;               /* Hold Vertex normal into the solid. */
  131.     int Color;
  132.     ByteType Count, Tags;                 /* Some attributes. */
  133. } IPVertexStruct;
  134.  
  135. /*****************************************************************************
  136. * Polygon Type - holds single polygon - Its Plane definition, and a pointer  *
  137. * to its vertices contour list V. As for IPVertexStruct, different attributes*
  138. * can be saved in Tags. PAux can be used locally by different modules, for   *
  139. * local usage only, and nothing sould be assumed on entry.             *
  140. *****************************************************************************/
  141.  
  142. /* Note bits 4-7 (high nibble of Tags) are reserved for the different         */
  143. /* modules to perform their local tasks and so should not be used here.         */
  144. #define IP_POLY_CONVEX_TAG    0x01       /* Convex Tag - Set if is convex. */
  145. #define IP_POLY_BBOX_TAG    0x02  /* BBox Tag - Set if BBox is computed. */
  146. #define IP_POLY_PLANE_TAG    0x04    /* Plane Tag - set of has plane def. */
  147.  
  148. #define    IP_IS_CONVEX_POLY(Poly)        ((Poly) -> Tags & IP_POLY_CONVEX_TAG)
  149. #define    IP_SET_CONVEX_POLY(Poly)    ((Poly) -> Tags |= IP_POLY_CONVEX_TAG)
  150. #define    IP_RST_CONVEX_POLY(Poly)    ((Poly) -> Tags &= ~IP_POLY_CONVEX_TAG)
  151. #define    IP_HAS_BBOX_POLY(Poly)        ((Poly) -> Tags & IP_POLY_BBOX_TAG)
  152. #define    IP_SET_BBOX_POLY(Poly)        ((Poly) -> Tags |= IP_POLY_BBOX_TAG)
  153. #define    IP_RST_BBOX_POLY(Poly)        ((Poly) -> Tags &= ~IP_POLY_BBOX_TAG)
  154. #define IP_HAS_PLANE_POLY(Poly)        ((Poly) -> Tags & IP_POLY_PLANE_TAG)
  155. #define IP_SET_PLANE_POLY(Poly)        ((Poly) -> Tags |= IP_POLY_PLANE_TAG)
  156. #define IP_RST_PLANE_POLY(Poly)        ((Poly) -> Tags &= ~IP_POLY_PLANE_TAG)
  157.  
  158. typedef struct IPPolygonStruct {
  159.     struct IPPolygonStruct *Pnext;                /* To next in chain. */
  160.     struct IPAttributeStruct *Attrs;
  161.     VoidPtr PAux;
  162.     PlaneType Plane;             /* Holds Plane as Ax + By + Cz + D. */
  163.     BBoxType BBox;                    /* BBox of polygons. */
  164.     IPVertexStruct *PVertex;                    /* To vertices list. */
  165.     ByteType Count, Tags;                     /* Some attributes. */
  166. } IPPolygonStruct;
  167.  
  168. /*****************************************************************************
  169. * Object Type - main system structure, which holds all the objects defined   *
  170. * in the system like Numeric, Geometric etc.                     *
  171. *   Note that as the number of objects will be usually extermely low (100 is *
  172. * high estimate!) we can waste some memory here...                 *
  173. *****************************************************************************/
  174.  
  175. #define IP_IS_UNDEF_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_UNDEF)
  176. #define IP_IS_POLY_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_POLY)
  177. #define IP_IS_NUM_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_NUMERIC)
  178. #define IP_IS_POINT_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_POINT)
  179. #define IP_IS_VEC_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_VECTOR)
  180. #define IP_IS_PLANE_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_PLANE)
  181. #define IP_IS_CTLPT_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_CTLPT)
  182. #define IP_IS_MAT_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_MATRIX)
  183. #define IP_IS_STR_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_STRING)
  184. #define IP_IS_OLST_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_LIST_OBJ)
  185. #define IP_IS_CRV_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_CURVE)
  186. #define IP_IS_SRF_OBJ(Obj)    ((Obj) -> ObjType == IP_OBJ_SURFACE)
  187.  
  188. #define IP_IS_GEOM_OBJ(Obj)    (IP_IS_UNDEF_OBJ(Obj) || \
  189.                  IP_IS_POLY_OBJ(Obj) || \
  190.                  IP_IS_POINT_OBJ(Obj) || \
  191.                  IP_IS_CTLPT_OBJ(Obj) || \
  192.                  IP_IS_VEC_OBJ(Obj) || \
  193.                  IP_IS_CRV_OBJ(Obj) || \
  194.                  IP_IS_SRF_OBJ(Obj))
  195.  
  196. #define IP_IS_POLYGON_OBJ(Obj)      (((Obj) -> Tags & 0x03) == 0)
  197. #define IP_SET_POLYGON_OBJ(Obj)      ((Obj) -> Tags = ((Obj) -> Tags & 0xfc))
  198. #define IP_IS_POLYLINE_OBJ(Obj)      (((Obj) -> Tags & 0x03) == 1)
  199. #define IP_SET_POLYLINE_OBJ(Obj)  ((Obj) -> Tags = ((Obj) -> Tags & 0xfc) + 1)
  200. #define IP_IS_POINTLIST_OBJ(Obj)  (((Obj) -> Tags & 0x03) == 2)
  201. #define IP_SET_POINTLIST_OBJ(Obj) ((Obj) -> Tags = ((Obj) -> Tags & 0xfc) + 2)
  202.  
  203. /* Maximum size of object list to start with (reallocated dynamically). */
  204. #define MAX_OBJ_LIST    10
  205.  
  206. typedef struct IPObjectStruct {
  207.     struct IPObjectStruct *Pnext;                /* To next in chain. */
  208.     struct IPAttributeStruct *Attrs;
  209.     char Name[OBJ_NAME_LEN];                  /* Name of object. */
  210.     IPObjStructType ObjType;        /* Object Type: Numeric, Geometric, etc. */
  211.     ByteType Count;           /* Count Number of references to this object. */
  212.     unsigned int Tags;                         /* Some attributes. */
  213.     union {
  214.     IPPolygonStruct *Pl;                          /* Polygon/line list. */
  215.     CagdCrvStruct *Crvs;                  /* Free form curve(s). */
  216.     CagdSrfStruct *Srfs;                /* Free form surface(s). */
  217.     RealType R;                       /* Numeric real data. */
  218.     PointType Pt;                     /* Numeric real point data. */
  219.     VectorType Vec;                    /* Numeric real vector data. */
  220.     PlaneType Plane;                 /* Numeric real plane data. */
  221.     CagdCtlPtStruct CtlPt;                  /* Control point data. */
  222.     MatrixType *Mat;        /* Numeric 4 by 4 transformation matrix. */
  223.     struct {
  224.         struct IPObjectStruct **PObjList;         /* List of objects. */
  225.         int ListMaxLen;          /* Maximum number of elements in list. */
  226.     } Lst;
  227.     char *Str;              /* General string for text object. */
  228.     VoidPtr *VPtr;
  229.     } U;
  230. } IPObjectStruct;
  231.  
  232. typedef void (*IritPrsrPrintFuncType)(char *);
  233.  
  234. extern jmp_buf
  235.     _IritPrsrLongJumpBuffer;
  236. extern int
  237.     _IritPrsrPolyListCirc,
  238.     _IritPrsrReadSocket,
  239.     _IritPrsrWriteSocket,
  240.     _IritPrsrReadWriteBinary;
  241.  
  242. #if defined(__cplusplus) || defined(c_plusplus)
  243. extern "C" {
  244. #endif
  245. FILE *IritPrsrOpenDataFile(char *FileName, int Read, int Messages);
  246. void IritPrsrCloseDataFile(FILE *f);
  247. IPObjectStruct *IritPrsrGetDataFiles(char **DataFileNames, int NumOfDataFiles,
  248.                      int Messages, int MoreMessages);
  249. IPObjectStruct *IritPrsrGetObjects(FILE *f);
  250. IPObjectStruct *IritPrsrProcessReadObject(IPObjectStruct *PObj);
  251. void IritPrsrStdoutObject(IPObjectStruct *PObj);
  252. void IritPrsrPutObject(FILE *f, IPObjectStruct *PObj);
  253. int IritPrsrParseError(char **ErrorMsg);
  254. void IritPrsrSetPrintFunc(IritPrsrPrintFuncType PrintFunc);
  255. void IritPrsrSetFloatFormat(char *FloatFormat);
  256. void IritPrsrSetBinaryFormat(int BinFormat);
  257. void IritPrsrPropagateAttrs(IPObjectStruct *PObj, IPAttributeStruct *Attrs);
  258. void IritPrsrReadSocket(int ReadSocket);
  259. void IritPrsrWriteSocket(int WriteSocket);
  260. void IritPrsrInputUnGetC(char c);
  261. void IritPrsrSetPolyListCirc(int Circ);
  262. void IritPrsrSetFlattenObjects(int Flatten);
  263. void IritPrsrSetReadOneObject(int OneObject);
  264. void IritPrsrFatalError(char *Msg);
  265. void IritPrsrUpdatePolyPlane(IPPolygonStruct *PPoly);
  266. void IritPrsrUpdatePolyPlane2(IPPolygonStruct *PPoly, VectorType Vin);
  267. void IritPrsrUpdateVrtxNrml(IPPolygonStruct *PPoly, VectorType DefNrml);
  268. IPObjectStruct *IritPrsrReverseObjList(IPObjectStruct *PObj);
  269. void IritPrsrReverseVrtxList(IPPolygonStruct *Pl);
  270. void IritPrsrParserAbort(IritPrsrErrType ErrNum, char *Msg);
  271.  
  272. /* Binary stream functions. */
  273. IPObjectStruct *IritPrsrGetBinObject(void *f);
  274. void IritPrsrPutBinObject(void *f, IPObjectStruct *PObj);
  275.  
  276. /* If set to TRUE (default) polygons will have their vertex lists circular   */
  277. /* or last vertex point on first. If set to FALSE, regular list list.         */
  278. extern int IritPrsrPolyListCirc;
  279.  
  280. /* Will be set to VIEW_MAT and PERS_MAT respectively if found in parsed data.*/
  281. extern MatrixType IritPrsrViewMat, IritPrsrPrspMat;
  282. extern int IritPrsrWasViewMat, IritPrsrWasPrspMat;
  283.  
  284. /* Variable is used internally. It should not be accessed by applications. */
  285. extern int _IritPrsrPolyListCirc;
  286.  
  287. /* External variables/functions that should be provided by the application:  */
  288.  
  289. /* Gets two list of all curves and surfaces in the datafile and process them */
  290. /* as needed. May return a processed version to be put on returned list from */
  291. /* IritPrsrGetObjects (polygonal approximation of the free form data for     */
  292. /* example), or NULL otherwise.                             */
  293. /* This function is responsible to free crvs/srfs if not needed any more.    */
  294. IPObjectStruct *IritPrsrProcessFreeForm(IPObjectStruct *CrvObjs,
  295.                     IPObjectStruct *SrfObjs);
  296.  
  297. /* Convexity test for a polygon. */
  298. int IritPrsrIsConvexPolygon(IPPolygonStruct *Pl);
  299.  
  300. /* Last element retrieval routines. */
  301. IPObjectStruct *IritPrsrGetLastObj(IPObjectStruct *OList);
  302. IPObjectStruct *IritPrsrGetPrevObj(IPObjectStruct *OList, IPObjectStruct *O);
  303. IPPolygonStruct *IritPrsrGetLastPoly(IPPolygonStruct *PList);
  304. IPPolygonStruct *IritPrsrGetPrevPoly(IPPolygonStruct *PList, IPPolygonStruct *P);
  305. IPVertexStruct *IritPrsrGetLastVrtx(IPVertexStruct *VList);
  306. IPVertexStruct *IritPrsrGetPrevVrtx(IPVertexStruct *VList, IPVertexStruct *V);
  307. int IritPrsrObjListLen(IPObjectStruct *O);
  308. int IritPrsrPolyListLen(IPPolygonStruct *P);
  309. int IritPrsrVrtxListLen(IPVertexStruct *V);
  310.  
  311. #if defined(__cplusplus) || defined(c_plusplus)
  312. }
  313. #endif
  314.  
  315. #endif    /* IRIT_PRSR_GH */
  316.